<?php

namespace App\Http\Controllers\Admin\Order;

use App\Exceptions\OrderException;
use App\Services\Admin\Order\ShopOrderService;
use Exception;
use Illuminate\Http\JsonResponse;

class ShopOrderController extends BaseOrderController
{
    protected ShopOrderService $shopOrderService;

    /**
     * @throws Exception
     */
    public function __construct()
    {
        parent::__construct();
        $this->shopOrderService = $this->loadService('Admin\Order\ShopOrderService');
    }

    /**
     * 商品订单列表
     * @return JsonResponse
     * @throws Exception
     */
    public function list(): JsonResponse
    {
        $list = $this->shopOrderService->list($this->params);
        return outputJsonResult($list);
    }

    /**
     * 商品订单详情
     * @return JsonResponse
     * @throws OrderException
     */
    public function detail(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $detail = $this->shopOrderService->detail($this->params);
        return outputJsonResult($detail);
    }

    /**
     * 操作发货
     * @return JsonResponse
     * @throws Exception
     */
    public function delivery(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
            "expressCompanyCode" => "required|string|min:1",
            'expressNo' => 'required|string|min:1',
            'phone' => 'required|string|min:11,max:11',
            'remark' => 'max:128'
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->shopOrderService->delivery($this->params);
        return outputJsonResult();
    }

    /**
     * 更新订单状态
     * @return JsonResponse
     * @throws OrderException
     */
    public function update(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
            'orderStatus' => 'required|integer|in:30',
            'remark' => 'required|string'
        ];
        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->shopOrderService->update($this->params);
        return outputJsonResult();
    }
}
